package com.likou;

/**
 * Author: 陈桂明
 * Date: 2025/9/10 17:14
 * Description: q124
 */
public class q124 {

    Integer max = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        DFS(root);

        return max;
    }

    public Integer DFS(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftMax = Math.max(root.val, root.val + DFS(root.left));
        int rightMax = Math.max(root.val, root.val + DFS(root.right));
        int l = leftMax + rightMax - root.val;
        max = Math.max(max, l);

        return Math.max(leftMax, rightMax);
    }
}
